{{--继承模板--}}
@extends('admin.layouts.common')
@section('content')
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-bottom:none;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <select id="types">
                         <option value="">搜索类型</option>
                         <option value="id">编号</option>
                         <option value="title">权限名称</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="key" id="key" placeholder="请输入查询内容">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-black" id="search">
                        <i class="layui-icon layuiadmin-button-btn">&#xe615;</i>
                    </button>
                    <button  tipsmsg="添加" class="layui-btn layui-btn-blue onmousemovebtns" onclick="x_admin_show('添加权限','{{url('admin/auth/ruleadd')}}','750px','520px','95%','95%')"><i class="layui-icon layuiadmin-button-btn"></i></button>
                    <button tipsmsg="展开或折叠全部" class="layui-btn layui-btn-red onmousemovebtns" onclick="openAll();">
                        <i class="layui-icon layuiadmin-button-btn">&#xe857;</i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body" style="margin-top: -20px;">
            <table class="layui-hide" id="tableId" lay-filter="tableId"></table>
        </div>
    </div>
    <script type="text/html" id="addtime">
    @{{layui.util.toDateString(d.addtime*1000,'yyyy-MM-dd HH:mm:ss')}}
    </script>
    <script type="text/html" id="levels">
        @{{# for (var i=0;i < d.levels ;i++){ }}
        <i class="iconfont icon-xingxing3" style="color:#FDA736"></i>
        @{{# } }}
    </script>
    <script type="text/html" id="status">
        <input type="checkbox" name="menustatus" value="@{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="menustatus" @{{ d.menustatus == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="order">
        <input name="@{{d.id}}" data-id="@{{d.id}}" class="list_order layui-input" value="@{{d.sort}}" size="10" style="width: 45px;height: 25px;" />
    </script>
    <script type="text/html" id="icon">
        <i class="iconfont @{{d.icon}}"></i>
    </script>
    <script type="text/html" id="action">
        <span tipsmsg="添加子菜单" class="layuimsg" onclick="x_admin_show('添加子菜单','{{ url('admin/auth/ruleadd')}}?pid=@{{d.id }}','750px','520px','95%','95%')">
            <button class="layui-btn layui-btns-blue layui-btn-xs">
                <i class="iconfont icon-tianjia2"></i>
            </button>
        </span>
        <span>
            <button tipsmsg="编辑权限" class="layui-btn layui-btns-black layui-btn-xs layuimsg" onclick="x_admin_show('编辑权限','{{ url('admin/auth/ruleedit')}}?id=@{{d.id }}','750px','490px','95%','95%')">
                <i class="iconfont icon-bianji2"></i>
            </button>
        </span>
        <span>
            <button tipsmsg="删除" class="layui-btn layui-btns-red layui-btn-xs layuimsg"  lay-event="del">
                <i class="iconfont icon-shanchu-tianchong"></i>
            </button>
        </span>
    </script>
    <script src="{{asset('layuiadmin/layui/layui.js')}}"></script>
    <script src="{{asset('admin/js/jquery.2.1.1.min.js')}}"></script>
    <script src="{{asset('admin/js/common.js')}}"></script>
    <script>
        layui.use(['util'], function(){
            var util = layui.util;
            //固定块
            util.fixbar({
                bar1: '&#xe669'
                ,css: {right: 25, bottom: 25}
                ,bgcolor: '#393D39'
                ,click: function(type){
                    if(type === 'bar1'){
                        window.location.reload();
                    }else{

                    }
                }
            });
        });
    </script>
    <script>
        var editObj=null,ptable=null,treeGrid=null,tableId='tableId',layer=null;
        var _token =  $('meta[name="csrf-token"]').attr('content');
        layui.config({
            base: "/layuiadmin/layui/extend/"
        }).extend({
            treeGrid:'treeGrid'
        }).use(['jquery','treeGrid','layer','form','element'], function(){
            var $=layui.jquery;
            treeGrid = layui.treeGrid;
            layer=layui.layer;
            element = layui.element;
            form = layui.form;
            ptable=treeGrid.render({
                id:tableId
                ,elem: '#'+tableId
                ,idField:'id'
                ,url: "{{url('admin/auth/authrule')}}"
                ,text:{none: '<i class="fa fa-snowflake-o" aria-hidden="true"></i>&nbsp;暂无相关数据'}
                ,cellMinWidth: 100
                ,treeId:'id'//树形id字段名称
                ,treeUpId:'pid'//树形父id字段名称
                ,treeShowName:'title'//以树形式显示的字段
                ,height:'full-140'
                ,where:{_token:_token}
                ,isFilter:false
                ,iconOpen:true//是否显示图标【默认显示】
                ,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
                ,cols: [[
                    {field: 'id', title: '编号', width: 100,sort:true,align:'center',sort:true},
                    {field: 'icon', align: 'center',title: '图标', width: 80,templet: '#icon'},
                    {field: 'title', title: '权限名称'},
                    {field: 'href', title: '控制器/方法'},
                    {field: 'levels', title: '层级', width: 100,toolbar: '#levels'},
                    {field: 'menustatus',align: 'center',title: '菜单状态', width: 120,toolbar: '#status'},
                    {field: 'sort',align: 'center', title: '排序', width: 120,templet: '#order',sort:true},
                    {title:'操作',width: 120, toolbar: '#action'}
                ]]
                ,limits:[{{$infomsg['configinfo']['limit']}},100,200,400,600,1000,2000]
                ,limit:{{$infomsg['configinfo']['limit']}}//每页默认显示的数量
                ,page:true
                ,done:function(){
                    onmousemoves();
                }
            });

            $('#search').click(function() {
                //搜索
                var key = $('#key').val();
                var types = $('#types').val();
                if($.trim(key) != '' && types == 0){
                    layer.msg('请选择搜索类型！');
                    return false;
                }
                if($.trim(key) == '' && types != 0){
                    layer.msg('搜索内容不能为空！');
                    return false;
                }
                treeGrid.reload('tableId',{
                    where: {key: key,types:types,_token:_token},
                })

            })

            //修改菜单状态
            form.on('switch(menustatus)', function(obj){
                var id = this.value;
                var menustatus = obj.elem.checked===true?1:0;
                $.post("{{url('admin/auth/menustatus')}}",{'id':id,'menustatus':menustatus,_token:_token},function(msg) {
                    if(msg.code == 1){
                        layer.msg(msg.msg,function(){
                            window.location.reload();
                        });
                    }else if(msg.code == 0) {
                        layer.msg(msg.msg);
                    }else{
                        layer.msg('你无操作权限！');
                        window.location.reload();
                    }
                },'json');
            });

            //单条信息删除
            treeGrid.on('tool('+tableId+')', function(obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('您确定要删除编号为： '+ data.id + ' 信息吗？', {icon: 3,skin: 'layui-layer-lan',anim: 4,title:'删除'}, function(index){
                        $.post("{{url('admin/auth/ruledel')}}",{id:data.id,_token:_token},function(res){
                            if(res.code===1){
                                layer.msg(res.msg);
                                obj.del();
                            }else if(res.code === 0) {
                                layer.msg(res.msg);
                            }else{
                                layer.msg('你无操作权限！');
                            }
                        },'json');
                        layer.close(index);
                    });
                }
            });

            $('body').on('blur','.list_order',function() {
                var id = $(this).attr('data-id');
                var sort = $(this).val();
                $.post("{{url('admin/auth/rulesort')}}",{id:id,sort:sort,_token:_token},function(res){
                    if(res.code==1){
                        layer.msg(res.msg,function(){
                            window.location.reload();
                        });
                    }else if(res.code === 0) {
                        layer.msg(res.msg);
                          window.location.reload();
                    }else{
                        layer.msg('你无操作权限！');
                          window.location.reload();
                    }
                })
            })
        });

        function openAll() {
            var treedata=treeGrid.getDataTreeList(tableId);
            treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
        }
    </script>
@endsection